{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Exercícios\n",
    "\n",
    "1 - Aplique os algoritmos K-means [1] e AgglomerativeClustering [2] em qualquer dataset que você desejar (recomendação: iris). Compare os resultados utilizando métricas de avaliação de clusteres (completeness e homogeneity, por exemplo) [3].\n",
    "\n",
    "* [1] http://scikit-learn.org/stable/modules/clustering.html#k-means\n",
    "\n",
    "* [2] http://scikit-learn.org/0.17/modules/clustering.html#hierarchical-clustering\n",
    "\n",
    "* [3] http://scikit-learn.org/stable/modules/clustering.html#clustering-evaluation\n",
    "\n",
    "2 - Qual o valor de K (número de clusteres) você escolheu para a questão anterior? Desenvolva o Método do Cotovelo (não utilizar lib!) e descubra o K mais adequado. Após descobrir, aplique novamente o K-means com o K adequado. \n",
    "\n",
    "* Ajuda: atributos do [k-means](http://scikit-learn.org/0.17/modules/generated/sklearn.cluster.KMeans.html#sklearn.cluster.KMeans)\n",
    "\n",
    "3 - Após a questão 2, você aplicou o algoritmo com K apropriado. Refaça o cálculo das métricas de acordo com os resultados de clusters obtidos com a questão anterior e verifique se o resultado melhorou.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.cluster import KMeans\n",
    "from sklearn.cluster import AgglomerativeClustering\n",
    "from sklearn import metrics\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data =  pd.read_csv('iris.data', header=None)\n",
    "clusters = 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "data[4] = data[4].astype('category')\n",
    "data[4] = data[4].cat.codes\n",
    "x = data.iloc[:,:4]\n",
    "y = data.iloc[:,-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "kmeans = KMeans(n_clusters=clusters, random_state=0).fit(x)\n",
    "predkmeans = kmeans.predict(x)\n",
    "\n",
    "aggclu = AgglomerativeClustering(n_clusters=clusters).fit(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "homogeneity kmeans with 4 clusters: 0.808314\n",
      "completeness kmeans with 4 clusters: 0.652211\n",
      "\n",
      "\n",
      "homogeneity agglomerative clustering with 4 clusters: 0.794794\n",
      "completeness agglomerative clustering with 4 clusters: 0.641884\n"
     ]
    }
   ],
   "source": [
    "print('homogeneity kmeans with {:d} clusters: {:f}'.format(clusters, metrics.homogeneity_score(y, predkmeans)))\n",
    "print('completeness kmeans with {:d} clusters: {:f}'.format(clusters, metrics.completeness_score(y,predkmeans)))\n",
    "print ('\\n')\n",
    "print('homogeneity agglomerative clustering with {:d} clusters: {:f}'.format(clusters, metrics.homogeneity_score(y, aggclu.labels_)))\n",
    "print('completeness agglomerative clustering with {:d} clusters: {:f}'.format(clusters, metrics.completeness_score(y,aggclu.labels_)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl0nfV95/H3V/tqWcvVYl0ZL9jCxhhbEmaJY5ICCQET\nA3YTk2mGtvSQnKY0bdPJJJm2M/NHmmTaMzNt0qSHrHSSYogNAVJCQkiCIUC8At53Y1u2NsuLFlvr\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GbV0U23ATE5geACDxDD\nwzZRh4CbzCwn2g+3Ebl2F5PMrDT6OJXI+Py/j9drpY3XH54A7wM+CWyJjnsDfMndnw8w06VUAI9F\nZy6kAE+6e8xPW4wTZcDTkX/bpAH/7u4vBBvpkh4BfhQdDtkP/FHAeS4p+uZ5B/CpoLNcirv/zsxW\nA5uAXmAzsX2X7BozKwZ6gM+M50X5uJ1eKSIiIxO3QzciIjIyKnoRkQSnohcRSXAqehGRBKeiFxFJ\ncCp6EZFoNTZ4AAAAE0lEQVQEp6IXEUlwKnoRkQT3/wE+XTgwwGt/bQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1a18475410>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "inertia = []\n",
    "for i in xrange(2,10):\n",
    "    kmeans.n_clusters = i\n",
    "    kmeans.fit(x)\n",
    "    inertia.append(kmeans.inertia_)\n",
    "    \n",
    "plt.plot(np.arange(2,10), inertia)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "logo, seguindo o metodo do cotovelo, o melhor número de clusters serão 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "clusters = 3\n",
    "kmeans = KMeans(n_clusters=clusters, random_state=0).fit(x)\n",
    "predkmeans = kmeans.predict(x)\n",
    "\n",
    "aggclu = AgglomerativeClustering(n_clusters=clusters).fit(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "homogeneity kmeans with 3 clusters: 0.751485\n",
      "completeness kmeans with 3 clusters: 0.764986\n",
      "\n",
      "\n",
      "homogeneity agglomerative clustering with 3 clusters: 0.760801\n",
      "completeness agglomerative clustering with 3 clusters: 0.779596\n"
     ]
    }
   ],
   "source": [
    "print('homogeneity kmeans with {:d} clusters: {:f}'.format(clusters, metrics.homogeneity_score(y, predkmeans)))\n",
    "print('completeness kmeans with {:d} clusters: {:f}'.format(clusters, metrics.completeness_score(y,predkmeans)))\n",
    "print ('\\n')\n",
    "print('homogeneity agglomerative clustering with {:d} clusters: {:f}'.format(clusters, metrics.homogeneity_score(y, aggclu.labels_)))\n",
    "print('completeness agglomerative clustering with {:d} clusters: {:f}'.format(clusters, metrics.completeness_score(y,aggclu.labels_)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
